package org.video.mine.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.GridLayoutManager;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.video.mine.MyPreference;
import org.video.mine.R;
import org.video.mine.adapter.HistoryAdapter;
import org.video.mine.databinding.HistoryActivityLayoutBinding;
import org.video.mine.entity.AlreadyLoadOkEntity;
import org.video.mine.entity.LoadedEntity;
import org.video.mine.entity.LookHistoryEntity;
import org.video.mine.event.DeleteEvent;
import org.video.mine.event.RefreshHistoryEvent;
import org.video.mine.fragment.MineViewModel;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class HistoryActivity extends AppCompatActivity {

    private MineViewModel mViewModel;
    private HistoryActivityLayoutBinding binding;
    private MyPreference preference;
    private Context context;
    private List<LookHistoryEntity> historyEntities;
    private List<LookHistoryEntity> adapterDatas = new ArrayList<>();
    private HistoryAdapter historyAdapter;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 0:
                    if (historyAdapter!=null){
                        historyAdapter.notifyDataSetChanged();
                    }
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.history_activity_layout);
        setContentView(binding.getRoot());

        context = this;
        mViewModel = ViewModelProviders.of(this).get(MineViewModel.class);
        // TODO: Use the ViewModel

        EventBus.getDefault().register(this);
        preference = MyPreference.getInstance();
        preference.setPreference(context);
        historyEntities = (List<LookHistoryEntity>) preference.getObject(MyPreference.history,LookHistoryEntity.class);
        if (historyEntities!=null){
            Collections.reverse(historyEntities);
            adapterDatas.addAll(historyEntities);
        }
        initView();
    }

    private void initView() {
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,1);
        binding.recycle.setLayoutManager(gridLayoutManager);
        historyAdapter = new HistoryAdapter(adapterDatas, context);
        binding.recycle.setAdapter(historyAdapter);
    }

    @Subscribe
    public void deleteHistory(DeleteEvent event){
        if (event!=null){
            adapterDatas.remove(event.pos);
            historyAdapter.notifyItemRemoved(event.pos);
//            mHandler.sendEmptyMessage(0);
            historyAdapter.notifyItemRangeRemoved(event.pos,adapterDatas.size());
        }
    }

    @Subscribe
    public void refreshHistory(RefreshHistoryEvent event){
        if (event!=null){
            historyEntities = (List<LookHistoryEntity>) preference.getObject(MyPreference.history,LookHistoryEntity.class);
            if (historyEntities!=null){
                adapterDatas.clear();
                Collections.reverse(historyEntities);
                adapterDatas.addAll(historyEntities);
            }
            List<AlreadyLoadOkEntity> loadOkEntities = (List<AlreadyLoadOkEntity>) preference.getObject(MyPreference.loadok, AlreadyLoadOkEntity.class);
            if (loadOkEntities!=null && loadOkEntities.size()>0 && adapterDatas!=null && adapterDatas.size()>0){
                for (LookHistoryEntity loadedEntity:adapterDatas) {
                    for (AlreadyLoadOkEntity loadOkEntity : loadOkEntities) {
                        if (loadOkEntity.getName().equals(loadedEntity.getName())){
                            loadedEntity.setImgUrl(loadOkEntity.getImgUrl());
                            break;
                        }
                    }
                }
            }
            mHandler.sendEmptyMessage(0);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
